<%@page import="java.util.List"%>
<%@page import="search.OrderSearchData"%>
<%@page import="java.util.Locale"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="helpers.DateHelper"%>
<%@page import="models.GeoModel"%>
<%@page import="admin.data.GeoData"%>
<%@page import="models.OrderModel"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="helpers.PaginatorHelper"%>
<%@page import="helpers.UrlHelper"%>
<%@page import="java.util.ArrayList"%>
<%
	OrderSearchData orderSearch = (OrderSearchData) request.getAttribute("orderSearch");
	String url = (String)request.getAttribute("url");
	GeoData geoData = new GeoData();
	List<OrderModel> orders = orderSearch.getListResult();
	String parameter = "";
	String email = "";
	String shipName = "";
	String shipPhoneNumber = "";
	String orderId = "";
	
	if(request.getAttribute("email")!=null&&!request.getAttribute("email").toString().isEmpty()){
		email = (String)request.getAttribute("email");
		parameter += "&email=" + email;
	}	
	if(request.getAttribute("shipName")!=null&&!request.getAttribute("shipName").toString().isEmpty()){
		shipName = (String)request.getAttribute("shipName");
		parameter += "&shipName=" + shipName;
	}	
	if(request.getAttribute("shipPhoneNumber")!=null&&!request.getAttribute("shipPhoneNumber").toString().isEmpty()){
		shipPhoneNumber = (String)request.getAttribute("shipPhoneNumber");
		parameter += "&shipPhoneNumber=" + shipPhoneNumber;
	}
	if(request.getAttribute("orderId")!=null&&!request.getAttribute("orderId").toString().isEmpty()){
		orderId = (String)request.getAttribute("orderId");
		parameter += "&orderId=" + orderId;
	}	
		
	int totalMoney = 0;
	NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
%>

<jsp:include page="/admin/layouts/header.jsp">
    <jsp:param name="language" value="1" />
</jsp:include>
<script type="text/javascript" src="/admin/js/list_form.js"></script>
<script type="text/javascript" src="/admin/js/list_order.js"></script>
<script type="text/javascript" src="/admin/js/form-user.js"></script>
<script type="text/javascript">
	var server = "<%=UrlHelper.getServerUrl(request)%>";
	jQuery(document).ready(function () {
		jQuery(".leftmenu ul li").removeClass("current");
		jQuery(".leftmenu ul li a.sell").parent().addClass("current");
	});
</script>

        <div class="maincontent noright">
        	<div class="maincontentinner">
            	
                <ul class="maintabmenu">
                	<li class="current"><a href="/admin/dashboard">Bán hàng trực tiếp</a></li>
                </ul><!--maintabmenu-->
                
                <div class="content">    
                <jsp:include page="/admin/elements/message.jsp"/>            	
                	<form class="stdform" action="/admin/sell/direct" method="post">
                    <div class="one_half">                          	                          	
                    	<p>
                    		<label>Email : </label>
                             <span class="field"><input type="text" name="email" class="longinput" value="<%=(email!=null&&!email.isEmpty())?email:""%>"/></span>                            
                    	</p>                       	           
						<p>
                    		<label>Họ tên : </label>
                             <span class="field"><input type="text" name="shipName" class="longinput" value="<%=(shipName!=null&&!shipName.isEmpty())?shipName:""%>"/></span>                            
                    	</p>                 	
                    	
                        <p>
                    		<label>Điện thoại : </label>
                             <span class="field"><input type="text" name="shipPhoneNumber" class="longinput" value="<%=(shipPhoneNumber!=null&&!shipPhoneNumber.isEmpty())?shipPhoneNumber:""%>"/></span>                            
                    	</p>
                    	<p>
                    		<label>Mã đơn hàng : </label>
                             <span class="field"><input type="text" name="orderId" class="longinput" value="<%=(orderId!=null&&!orderId.isEmpty())?orderId:""%>"/></span>                            
                    	</p>   
                   	                           	                    	       	                        					                    	
                    	<p class="stdformbutton">
                        	<input type="submit" class="reset radius2" value="Tìm kiếm">
                            <input type="reset" class="reset radius2" value="Làm lại" id="btnReset">                            
                        </p>                    	     	                    	        	                                         	
                    </div><!--one_half-->                   
                   </form> 
                   <br clear="all" /><br />                	
                	<div class="contenttitle radiusbottom0">
	                	<h2 class="table"><span>Danh sách Đơn hàng</span></h2>
	                </div>
	                
	                <div class="tableoptions">
	                    <select class="radius3 action-value">	                    			                    
	                        <option value="4">Chuyển trạng thái thành đã hoàn thành</option>
	                        <option value="7">Chuyển trạng thái thành kiểm tra lại</option>                      
	                        <option value="5">Huỷ tất cả đơn hàng</option>	                        
	                    </select> &nbsp;
	                    <button class="radius3 apply-change">Áp dụng</button>
	                </div>
	                
	                <table cellspacing="0" cellpadding="0" border="0" class="stdtable stdtablecb" id="list-table">
	                    <colgroup>
	                        <col class="con0">
	                        <col class="con1">
	                        <col class="con0">
	                        <col class="con1">
	                        <col class="con0">
	                        <col class="con1">
	                    </colgroup>
	                    <thead>
	                        <tr>
	                        	<th class="head0"><span class="checkbox"><input type="checkbox" class="checkall"></span></th>
	                            <th class="head1">Mã Đơn hàng</th>
	                            <th class="head0">Ngày đặt</th>
	                            <th class="head1">Tên Deal</th>
	                            <th class="head0">Thanh toán</th>
	                            <th class="head1">Người mua</th>
	                            <th class="head1">Địa chỉ</th>
	                            <th class="head0">Trạng thái</th>
	                            <th class="head1">Action</th>
	                        </tr>
	                    </thead>	                   
	                    <tbody>
	                    	<% 
	                    		for (int i = 0; i < orders.size(); i++) { 
	                    			totalMoney += orders.get(i).getTotalPrice();
	                    	%>
	                        <tr>
	                        	<td class="center">
	                        		<%
	                        			if(orders.get(i).getStatus()==1){
	                        		%>
	                        		<span class="checkbox"><input type="checkbox" value="<%=orders.get(i).getOrderId()%>"></span>
	                        		<%
	                        			}
	                        		%>
	                        	</td>
								<td class="center"><%=orders.get(i).getOrderId() %></td>
	                            <td><%=DateHelper.dateToString(orders.get(i).getOrderDate(),"dd/MM/yyyy")%></td>
	                            <td>
	                            	<p>(<%=orders.get(i).getQuantity() %>) x <%=orders.get(i).getDealId() %> - <%=orders.get(i).getDealShortName() %></p>	                            	
                            	</td>
                            	<td>
                            		<%
									switch (orders.get(i).getPaymentType()) {
										case 1: 
											%>Thanh toán trực tiếp<%
											break;
										case 2: 
											%>Thanh toán chuyển khoản<%
											break;
										default:
											%>Khác<%
											break;											
									}
									%>
									<p><i>Tổng cộng : <%= nf.format(orders.get(i).getTotalPrice()) %> VND.</i></p>
                            	</td>
	                            <td>
	                            	<p><a target="blank" href="/admin/order/list?email=<%=orders.get(i).getUsername() %>" title="Xem tất cả đơn hàng của user này"><%=orders.get(i).getShipName() %></a> - </p>	                            	
	                            	<p><a target="blank" href="/admin/order/list?shipPhoneNumber=<%=orders.get(i).getShipPhoneNumber() %>" title="Xem tất cả đơn hàng của số điện thoại này"><%=orders.get(i).getShipPhoneNumber() %></a></p>
                            	</td>
                            	<td>
                            		<p><%=orders.get(i).getShipAddressFull() %>.</p>
                            		<p><i><%=(orders.get(i).getNote()!=null&&orders.get(i).getNote().length()>0)?"\""+orders.get(i).getNote()+"\"":"" %></i></p>
                            	</td>
	                            <td class="center">
	                            	<%
									switch (orders.get(i).getStatus()) {
										case 1: 
											%>Chưa xử lý<%
											break;										
										case 2:
											%>Chờ giao<%
											break;
										case 3:
											%>Đang giao<%
											break;
										case 4:
											if(orders.get(i).isCheckOut()){
												%>Hoàn thành<%
											}else{
												%>Đã giao<%
											}
											break;
										case 7:
											%>Kiểm tra lại<%
											break;
										case 5:
											%>Đã huỷ<%
											break;	
										case 8:
											%>Đã khoá<%
											break;											
										case 10:
											%>Khách hẹn lại<%
											break;	
										default:
											%>Khác<%
											break;											
									}
									%>
	                            </td>
	                            <td class="center">
									<a title="Link" href="/admin/sell/direct?action=view&id=<%=orders.get(i).getOrderId()%>" class="edit">Xem</a>
									<%
										if(orders.get(i).getStatus()==1){
									%>
									&nbsp;|&nbsp;
									<a title="Finish Link" href="<%=orders.get(i).getOrderId()%>" id="action_finish" class="finish_button">Hoàn&nbsp;Thành</a>&nbsp;|&nbsp;
									<a title="Remove Link" href="<%=orders.get(i).getOrderId()%>" id="action_del" class="delete_button">Huỷ</a>
									<%
										}
									%>
								</td>
	                        </tr>
	                        <% } %>
	                    </tbody>
	                </table>
	                <div class="dataTables_paginate paging_full_numbers" id="dyntable_paginate">	 
	                	 <p><b>Tổng cộng</b> : <%= orderSearch.getTotalReturn()%>/<%= orderSearch.getTotalResult()%> (đơn hàng).</p>
	                	 <p><b>Tổng số tiền</b> : <%=nf.format(orderSearch.getRecentMoney()) %> / <%=nf.format(orderSearch.getTotalMoney()) %> (VND).</p>   
	                </div>
	                <!-- paginate -->
	               <div class="dataTables_paginate paging_full_numbers" id="dyntable_paginate">	 	               	                          
	                	<% if (orderSearch.isHasPreviousPage()) { %>
							<a class="previous paginate_button" id="dyntable_previous" 
							href="<%=url%>?page=<%=orderSearch.getPage()-1%><%=parameter%>">Trước</a>
						<% } else { %>
							<span class="previous paginate_button paginate_button_disabled" id="dyntable_previous">Trước</span>
						<% } %>
						<% if (orderSearch.isHasNextPage()) { %>
							<a class="next paginate_button" id="dyntable_next"  
							href="<%=url%>?page=<%=orderSearch.getPage()+1%><%=parameter%>">Sau</a>
						<% } else { %>
							<span class="next paginate_button paginate_button_disabled" id="dyntable_next">Sau</span>
						<% } %>
	                </div>
                </div><!--content-->
                
            </div><!--maincontentinner-->
            
            <jsp:include page="/admin/elements/copyright.jsp">
            	<jsp:param name="language" value="1" />
			</jsp:include>
            
        </div><!--maincontent-->
        
<jsp:include page="/admin/layouts/footer.jsp">
    <jsp:param name="language" value="1" />
</jsp:include>